package com.temp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.temp.dto.SearchCategoryDTO;
import com.temp.entity.Category;
import com.temp.mapper.CategoryMapper;
import com.temp.service.CategoryService;
import com.temp.vo.CustomPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public CustomPageVO<Category> findAllCategory(SearchCategoryDTO categoryDTO) {
        CustomPageVO<Category> customPageVO = new CustomPageVO<>();
        Integer count = Optional.ofNullable(categoryMapper.countAllCategory(categoryDTO)).orElse(0);
        customPageVO.setTotal(count);
        if(count < 1) {
            customPageVO.setDataList(Collections.emptyList());
            return customPageVO;
        }

        List<Category> permissions = categoryMapper.findAllCategory(categoryDTO);
        customPageVO.setDataList(permissions);
        return customPageVO;
    }
}
